package com.shangcheng.server.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.shangcheng.model.domain.PmsProductAttribute;
import com.shangcheng.model.domain.PmsProductAttributeCategory;
import com.shangcheng.model.vo.PageResult;
import com.shangcheng.model.vo.PmsProductAttributeAttrInfoVo;
import com.shangcheng.model.vo.result.Result;
import com.shangcheng.server.exception.BusinessException;
import com.shangcheng.server.service.PmsProductAttributeCategoryService;
import com.shangcheng.server.service.PmsProductAttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author maorf
 * @version 1.0
 */
@RestController
@RequestMapping("productAttribute")
public class PmsProductAttributeController {
    @Autowired
    private PmsProductAttributeService pmsProductAttributeService;
    @Autowired
    private PmsProductAttributeCategoryService pmsProductAttributeCategoryService;

    //根据商品分类的id获取商品属性及属性分类
    @GetMapping("attrInfo/{productCategoryId}")
    public ResponseEntity attrInfo(@PathVariable Integer productCategoryId) {
        List<PmsProductAttributeAttrInfoVo> pmsProductAttributeAttrInfoVo = pmsProductAttributeService.attrInfo(productCategoryId);
        return ResponseEntity.ok(new Result(200, pmsProductAttributeAttrInfoVo, ""));
    }

    //添加商品属性信息
    @PostMapping("create")
    public ResponseEntity create(@RequestBody PmsProductAttribute pmsProductAttribute) {
        boolean save = pmsProductAttributeService.savePmsProductAttribute(pmsProductAttribute);
        if (save) {
            PmsProductAttributeCategory pmsProductAttributeCategory = pmsProductAttributeCategoryService.getById(pmsProductAttribute.getProductAttributeCategoryId());
            if (pmsProductAttribute.getType() == 0) {
                pmsProductAttributeCategory.setAttributeCount(pmsProductAttributeCategory.getAttributeCount() + 1);
            } else {
                pmsProductAttributeCategory.setParamCount(pmsProductAttributeCategory.getParamCount() + 1);
            }
            pmsProductAttributeCategoryService.updateById(pmsProductAttributeCategory);
        }
        return ResponseEntity.ok(new Result(200, null, ""));
    }

    //批量删除商品属性
    @PostMapping("delete")
    public ResponseEntity delete(Integer[] ids) {
        PmsProductAttribute byId = pmsProductAttributeService.getById(ids[0]);
        pmsProductAttributeService.deleteByids(ids);
        PmsProductAttributeCategory category = pmsProductAttributeCategoryService.getById(byId.getProductAttributeCategoryId());
        if (byId.getType()==0){
            category.setAttributeCount(category.getAttributeCount()- ids.length);
        }else{
            category.setParamCount(category.getParamCount()- ids.length);
        }
        pmsProductAttributeCategoryService.updateById(category);
        return ResponseEntity.ok(new Result(200, null, ""));
    }

    //根据分类查询属性列表或参数列表
    @GetMapping("/list/{cid}")
    public ResponseEntity list(@PathVariable Integer cid,
                               @RequestParam(defaultValue = "1") Integer pageNum,
                               @RequestParam(defaultValue = "5") Integer pageSize,
                               Integer type) {
        PageResult result = pmsProductAttributeService.productAttribute(cid, pageNum, pageSize, type);
        return ResponseEntity.ok(new Result(200, result, ""));
    }

    //修改商品属性信息
    @PostMapping("/update/{id}")
    public ResponseEntity updatePmsProductAttribute(@PathVariable Integer id, @RequestBody PmsProductAttribute pmsProductAttribute) {
        pmsProductAttribute.setId(Long.valueOf(id));
        PmsProductAttribute one = pmsProductAttributeService.getOne(new LambdaQueryWrapper<PmsProductAttribute>().eq(PmsProductAttribute::getName, pmsProductAttribute.getName()));
        if (!ObjectUtils.isEmpty(one)&&!one.getId().equals(pmsProductAttribute.getId())){
            throw new BusinessException("该用户名已存在");
        }
        pmsProductAttributeService.updateById(pmsProductAttribute);
        return ResponseEntity.ok(new Result(200, null, ""));
    }

    //查询单个商品属性
    @GetMapping("/{id}")
    public ResponseEntity getPmsProductAttributeById(@PathVariable Integer id) {
        PmsProductAttribute pmsProductAttribute = pmsProductAttributeService.getById(id);
        return ResponseEntity.ok(new Result(200, pmsProductAttribute, ""));
    }
}
